package bairui.international.weixin.portal.miniapp.controller.position.position;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_Town;
import bairui.international.service.Service_Town;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 县级市列表
 *
 * @generated
 * 
 * @see STest_C_Position_TownList_Get 测试
 *
 */
@Controller
public class C_Position_TownList_Get
 extends SC_Position_TownList_Get 
{
	
	@Autowired private Service_Town service_Town;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request, 
		@PathVariable(name=Get.PathVariableName.CityId) long cityId,
		@RequestParam(required=false) Long productId
	) throws Exception {
		List<TB_Town> towns = service_Town.list(it->it.city.id.eq(cityId));
		if(null == productId){
			return towns
				.stream()
				.map(it->new Output(it.getId(), it.getName(), false))
				.collect(Collectors.toList());
		}
		// productId != null
		return towns
				.stream()
				.map(it->{
//					boolean flag = service_ProxyArea.exists(new ID_ProxyArea(new TB_Product(productId), it));
					if(false){
						return new Output(it.getId(), it.getName(), true);
					}else{
						return new Output(it.getId(), it.getName(), false);
					}
				})
				.collect(Collectors.toList());
	}
	
	@AllArgsConstructor
	private static class Output { 
		@Getter private Long id;
		@Getter private String name;
		@Getter private Boolean selected;
	}
	
}
